package com.sxt.practiceapplication.activities;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.sxt.practiceapplication.R;
import com.sxt.practiceapplication.base.BaseActivity;
import com.sxt.practiceapplication.base.BasePager;
import com.sxt.practiceapplication.databinding.ActivityMain2Binding;
import com.sxt.practiceapplication.pager.IndexPager;
import com.sxt.practiceapplication.pager.MinePager;
import com.sxt.practiceapplication.pager.StudyPager;
import com.sxt.practiceapplication.utils.LoginUser;

import java.util.ArrayList;
import java.util.List;




public class MainActivity extends BaseActivity {

    private ActivityMain2Binding inflate;

    private List<BasePager> basePagerList;

    private ViewPager viewPager;

    private RadioGroup radioGroup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = ActivityMain2Binding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());

        initView();

        initRadioGroup();
    }

    @Override
    protected void onResume() {
        super.onResume();
        LoginUser.loginStateCheck(this);
    }

    private void initRadioGroup() {
        radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
            if (checkedId == R.id.tab_index) {
                viewPager.setCurrentItem(0);
            } else if (checkedId == R.id.tab_study) {
                viewPager.setCurrentItem(1);
            } else if (checkedId == R.id.tab_mine) {
                viewPager.setCurrentItem(2);
            } else {
                viewPager.setCurrentItem(0);
            }
        });
        radioGroup.check(R.id.tab_index);
    }

    private void initView() {
        viewPager = inflate.mainContent;
        radioGroup = inflate.rgMain;

        basePagerList = new ArrayList<>(3);
        basePagerList.add(new IndexPager(this));
        basePagerList.add(new StudyPager(this));
        basePagerList.add(new MinePager(this));

        viewPager.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return basePagerList.size();
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, int position) {
                BasePager basePager = basePagerList.get(position);
                View rootView = basePager.rootView;
                container.addView(rootView);
                if (position == 0) {
                    basePager.initData();
                }
                return rootView;
            }
        });

        //监听某个页面被选中，初始化对应的页面数据
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //调用各个页面的init方法
                BasePager basePager = basePagerList.get(position);
                if(position == 1) {
                    basePager.doInitData();
                }else {
                    basePager.initData();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        basePagerList.get(0).doInitData();
    }


}